/*
 * Copyright (c) 2020-2021. the original authors and DEPSEA.ORG
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.depsea.oss.config;

import com.aliyun.oss.ClientConfiguration;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import org.depsea.oss.client.PresignedUrlGenerator;
import org.depsea.oss.client.aliyun.AliyunCustomerBucketOssTemplate;
import org.depsea.oss.client.aliyun.AliyunOssTemplate;
import org.depsea.oss.client.aliyun.AliyunPresignedUrlGenerator;
import org.depsea.oss.config.properties.AliyunProperties;
import org.depsea.oss.filter.FileUploadFilter;
import org.depsea.oss.strategy.FilenameHandleStrategy;
import org.depsea.oss.strategy.RandomFilenameHandleStrategyImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * 自动装配
 *
 * @author Wang Chengwei
 * @since 1.0.0 2021/11/30 15:37
 */
@Configuration
@EnableConfigurationProperties({
        AliyunProperties.class
})
@ConditionalOnClass(OSSClient.class)
public class AliyunOssConfiguration {

    @Bean
    @ConditionalOnMissingBean(ClientConfiguration.class)
    @ConditionalOnProperty(value = "spring.oss.aliyun.enable", havingValue = "true")
    public ClientConfiguration clientConfiguration() {
        return new ClientConfiguration();
    }

    @Bean
    @ConditionalOnMissingBean(OSSClient.class)
    @ConditionalOnProperty(value = "spring.oss.aliyun.enable", havingValue = "true")
    public OSSClient ossClient(AliyunProperties aliyunProperties) {
        CredentialsProvider credentialsProvider = new DefaultCredentialProvider(
                aliyunProperties.getAccessKey(), aliyunProperties.getSecretKey()
        );
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        return new OSSClient(aliyunProperties.getEndpoint(), credentialsProvider, clientConfiguration);
    }

    @Bean
    @ConditionalOnMissingBean(AliyunOssTemplate.class)
    @ConditionalOnBean(OSSClient.class)
    public AliyunOssTemplate aliyunOssTemplate(
            OSSClient ossClient, AliyunProperties aliyunProperties,
            @Autowired(required = false) FilenameHandleStrategy filenameHandleStrategy,
            @Autowired(required = false) List<FileUploadFilter> filterList) {
        AliyunOssTemplate aliyunOssTemplate = new AliyunOssTemplate(ossClient, aliyunProperties.getDefaultBucketName());
        if (filenameHandleStrategy != null) {
            aliyunOssTemplate.setFilenameHandleStrategy(filenameHandleStrategy);
        }
        aliyunOssTemplate.addFilters(filterList);
        return aliyunOssTemplate;
    }

    @Bean
    @ConditionalOnMissingBean(AliyunCustomerBucketOssTemplate.class)
    @ConditionalOnBean(OSSClient.class)
    public AliyunCustomerBucketOssTemplate aliyunCustomerBucketOssTemplate(
            OSSClient ossClient,
            @Autowired(required = false) FilenameHandleStrategy filenameHandleStrategy,
            @Autowired(required = false) List<FileUploadFilter> filterList) {
        AliyunCustomerBucketOssTemplate aliyunOssTemplate = new AliyunCustomerBucketOssTemplate(ossClient);
        if (filenameHandleStrategy != null) {
            aliyunOssTemplate.setFilenameHandleStrategy(new RandomFilenameHandleStrategyImpl());
        }
        aliyunOssTemplate.addFilters(filterList);
        return aliyunOssTemplate;
    }

    @Bean
    @ConditionalOnMissingBean(AliyunPresignedUrlGenerator.class)
    @ConditionalOnBean(OSSClient.class)
    public AliyunPresignedUrlGenerator aliyunPresignedUrlGenerator(AliyunProperties aliyunProperties, OSSClient ossClient) {
        return new AliyunPresignedUrlGenerator(ossClient, aliyunProperties.getDefaultBucketName());
    }

}
